app.register.controller('DeliverCtrl', function($scope, $rootScope,
		DeliverSer, AppUtil, $http, $q,$location,$routeParams) {
	loadFlag($routeParams.id,$scope);
	
	$scope.pagination = {
			currentPage : page =1,
			itemsPerPage : size =10
	};

	var myDate = new Date(); 
    function addzero(v) {if (v < 10) return '0' + v;return v.toString();}
	var today = myDate.getFullYear().toString() + '-'+addzero(myDate.getMonth() + 1) + '-' +addzero(myDate.getDate());
	$("#send_date1").val(today);
	$("#send_date2").val(today);
	
	$scope.SearchData = function() {
		if($("#send_date1").val()=='' && $("#send_date2").val()==''){
			alertMsg("提示","发料时间不能为空");
			return false;
		}
		
		if(!($("#send_date1").val()!='' && $("#send_date2").val()!='')){
			alertMsg("提示","请同时输入日期范围中的开始日期和结束日期");
			return false;
		}
		
		if(($("#balance_date1").val()=='' && $("#balance_date2").val()!='') || ($("#balance_date1").val()!='' && $("#balance_date2").val()=='')){
			alertMsg("提示","请同时输入日期范围中的开始日期和结束日期");
			return false;
		}
		if(($("#square_date1").val()=='' && $("#square_date2").val()!='') || ($("#square_date1").val()!='' && $("#square_date2").val()=='')){
			alertMsg("提示","请同时输入日期范围中的开始日期和结束日期");
			return false;
		}
		if(($("#close_date1").val()=='' && $("#close_date2").val()!='') || ($("#close_date1").val()!='' && $("#close_date2").val()=='')){
			alertMsg("提示","请同时输入日期范围中的开始日期和结束日期");
			return false;
		}
		$scope.postData={};
		// 点击查询按钮时,恢复页码为1
		$scope.pagination.currentPage = 1;
		// 表单查询参数
		LoadList();
	};
	
	// 加载数据方法
	var LoadList = function() {
		if ($scope.postData == undefined)
			$scope.postData = {};
		var params = AppUtil.Params(".form_params");
		params = $.extend({}, $scope.selected,params);
		params["storage_text"] = angular.element('select[name=storage_code]').find("option:selected").text();
		params["part_model_group_code_set_text"] = angular.element('input[name=part_model_group_code_set]').val();
		// 当有查询参数时,重置页数
		if (AppUtil.Length(params) > 0) {
			if($scope.entity_name != null){
				params['entity_name'] = $scope.entity_name;
			}
			$scope.postData.params = params;
		}
		// 筛选查询参数
		var search_opt = AppUtil.GetSearchOpt();
		if (search_opt.length > 0) {
			$scope.postData.search_opt = search_opt;
		}
		
		$scope.postData.page = $scope.pagination.currentPage;
		$scope.postData.size = $scope.pagination.itemsPerPage;
		
		// 加载数据
		DeliverSer.list($scope.postData).success(function(response) {
			AppUtil.remove_loading();
			$scope.pagination.totalItems = response.total;
			$scope.deliverReports = response.data.data;
			$scope.deliverReportsSum = response.data.sum;	
			setTimeout(check_table_tr,500);
		});
	}
	
	$scope.ctrlDisabled =function(v){
		if(v == '12781001'){
			$("#close_date1").attr("disabled",false);
			$("#close_date2").attr("disabled",false);
		}else{
			$("#close_date1").val("");
			$("#close_date2").val("");
			$("#close_date1").attr("disabled",true);
			$("#close_date2").attr("disabled",true);
		}
	}
	
	// 配置分页监听
	$scope.$watch('pagination.refresh', LoadList);
	
	//加载查询条件
	$http.get("deliver/dicts").success(function(data){
		$scope.storage_list = data.data.storage_list;
		$scope.main_list = data.data.main_list;
		$scope.sub_list = data.data.sub_list;
		$scope.third_list = data.data.third_list;
		$scope.fourth_list = data.data.fourth_list;
		
		$scope.ro_status = data.data.ro_status;
		$scope.ro_type = data.data.ro_type;
		$scope.partmain_list = data.data.partmain_list;
		$scope.part_type = data.data.part_type;
		$scope.repair_type=data.data.repairType;
		$scope.is_colse = data.data.isornot;
		$scope.is_square = data.data.isornot;
		$scope.model_group = data.data.model_group;
		$scope.charge_partition = data.data.charge_partition;
		
		$scope.servicer = data.data.servicer;
		$scope.sender = data.data.sender;
		$scope.receiver = data.data.receiver;
		
		
		//收费区分
		$("#charge_partition_code").html("");
		$.each($scope.charge_partition,function(i,o){
			$("#charge_partition_code").append("<option value='"+o.charge_partition_code+"'>"+o.charge_partition_name+"</option>")
		})
		$("#charge_partition_code").multiselect({ 
			noneSelectedText: "==请选择==", 
			checkAllText: "全选", 
			uncheckAllText: '全不选', 
			selectedList:2,
			minWidth:"100%"
	    });
		$("#charge_partition_code").multiselect('refresh');
		
		//维修类型
		$("#repair_type_code").html("");
		$.each($scope.repair_type,function(i,o){
			$("#repair_type_code").append("<option value='"+o.repair_type_code+"'>"+o.repair_type_name+"</option>")
		})
		$("#repair_type_code").multiselect({ 
			noneSelectedText: "==请选择==", 
			checkAllText: "全选", 
			uncheckAllText: '全不选', 
			selectedList:2,
			minWidth:"100%"
	    });
		$("#repair_type_code").multiselect('refresh');
		
		//工单类别
		$("#ro_type_code").html("");
		$.each($scope.ro_type,function(i,o){
			$("#ro_type_code").append("<option value='"+o.status_code+"'>"+o.status_desc+"</option>")
		})
		$("#ro_type_code").multiselect({ 
			noneSelectedText: "==请选择==", 
			checkAllText: "全选", 
			uncheckAllText: '全不选', 
			selectedList:2,
			minWidth:"100%"
	    });
		$("#ro_type_code").multiselect('refresh');
		
		//其他
		$("#part_other").html("");
			$("#part_other").append("<option value='balanceNotClose'>已结算未关单</option>" +
					"<option value='outNotSquare'>已出库未结清</option>" +
					"<option value='outNotClose'>已出库未关单</option>" +
					"<option value='outNotBalance'>已出库未结算</option>")
		$("#part_other").multiselect({ 
			noneSelectedText: "==请选择==", 
			checkAllText: "全选", 
			uncheckAllText: '全不选', 
			selectedList:2,
			minWidth:"100%"
	    });
		$("#part_other").multiselect('refresh');
		
		
		$scope.pre_main_list=data.data.main_list;
		$scope.pre_sub_list=data.data.sub_list;
		$scope.pre_third_list=data.data.third_list;
		$scope.pre_fourth_list=data.data.fourth_list;
		$scope.pre_storage_list=data.data.storage_list;
		$scope.pre_servicer = data.data.servicer;
		$scope.pre_sender = data.data.sender;
		$scope.pre_receiver = data.data.receiver;
		
		$scope.pre_partmain_list = data.data.partmain_list;
		$scope.pre_repair_type=data.data.repairType;
		$scope.pre_model_group = data.data.model_group;
		$scope.pre_charge_partition = data.data.charge_partition;
	});
	
	$scope.LoadEntityLevel = function(entity) {
		$scope.currentEntity= entity.entity_code;
		$rootScope.search_entity_name = entity.entity_name;
		
		$scope.mainGroupName = '';
		$scope.subGroupName = '';
		$scope.thirdGroupName = '';
		$scope.subGroupName = '';
		$scope.storageName = '';
		
		$scope.main_list=null;
		$scope.sub_list=null;
		$scope.third_list_sel=null;
		$scope.fourth_list_sel=null;
		$scope.storage_list=null;
		//加载下拉列表数据
		$http.get("deliver/dicts?es="+entity.entity_code).success(function(data){
			$scope.main_list=data.data.main_list;
			$scope.sub_list=data.data.sub_list;
			$scope.third_list=data.data.third_list;
			$scope.fourth_list=data.data.fourth_list;
			$scope.storage_list=data.data.storage_list;
		

			$scope.ro_type = data.data.ro_type;
			$scope.partmain_list = data.data.partmain_list;
			$scope.repair_type=data.data.repairType;
			$scope.model_group = data.data.model_group;
			$scope.charge_partition = data.data.charge_partition;
			
			
			$scope.servicer = data.data.servicer;
			$scope.sender = data.data.sender;
			$scope.receiver = data.data.receiver;
			
		
			//收费区分
			$("#charge_partition_code").html("");
			$.each($scope.charge_partition,function(i,o){
				$("#charge_partition_code").append("<option value='"+o.charge_partition_code+"'>"+o.charge_partition_name+"</option>")
			})
			$("#charge_partition_code").multiselect({ 
				noneSelectedText: "==请选择==", 
				checkAllText: "全选", 
				uncheckAllText: '全不选', 
				selectedList:2,
				minWidth:"100%"
		    });
			$("#charge_partition_code").multiselect('refresh');
			
			//维修类型
			$("#repair_type_code").html("");
			$.each($scope.repair_type,function(i,o){
				$("#repair_type_code").append("<option value='"+o.repair_type_code+"'>"+o.repair_type_name+"</option>")
			})
			$("#repair_type_code").multiselect({ 
				noneSelectedText: "==请选择==", 
				checkAllText: "全选", 
				uncheckAllText: '全不选', 
				selectedList:2,
				minWidth:"100%"
		    });
			$("#repair_type_code").multiselect('refresh');
			
			//工单类别
			$("#ro_type_code").html("");
			$.each($scope.ro_type,function(i,o){
				$("#ro_type_code").append("<option value='"+o.status_code+"'>"+o.status_desc+"</option>")
			})
			$("#ro_type_code").multiselect({ 
				noneSelectedText: "==请选择==", 
				checkAllText: "全选", 
				uncheckAllText: '全不选', 
				selectedList:2,
				minWidth:"100%"
		    });
			$("#ro_type_code").multiselect('refresh');
			
			//其他
			$("#part_other").html("");
				$("#part_other").append("<option value='balanceNotClose'>已结算未关单</option>" +
						"<option value='outNotSquare'>已出库未结清</option>" +
						"<option value='outNotClose'>已出库未关单</option>" +
						"<option value='outNotBalance'>已出库未结算</option>")
			$("#part_other").multiselect({ 
				noneSelectedText: "==请选择==", 
				checkAllText: "全选", 
				uncheckAllText: '全不选', 
				selectedList:2,
				minWidth:"100%"
		    });
			$("#part_other").multiselect('refresh');
			
		})
		
		$("#maingroupname").val('');
		$("#subgroupname").val('');
		$("#thirdgroupname").val('');
		$("#fourthgroupname").val('');
		$("#storage_name").val('');
		$("#service_advisor_name").val('');
		$("#senderer").val('');
		$("#receiverer").val('');
		$("#part_model_group_code_set").val('');
	}
	
	//清空门店
	$scope.clearGroupBrandEntity = function(){
		$rootScope.search_entity_name='';
		
		$("#service_advisor_name").val('');
		$("#senderer").val('');
		$("#receiverer").val('');
		$("#part_model_group_code_set").val('');
		
		$scope.mainGroupName = '';
		$scope.subGroupName = '';
		$scope.thirdGroupName = '';
		$scope.subGroupName = '';
		$scope.storageName = '';
		
		$scope.main_list=null;
		$scope.sub_list=null;
		$scope.third_list_sel=null;
		$scope.fourth_list_sel=null;
		$scope.storage_list=null;
		$scope.servicer = null;
		$scope.sender = null;
		$scope.receiver = null;
		
		$scope.main_list=$scope.pre_main_list;
		$scope.sub_list=$scope.pre_sub_list;
		$scope.third_list=$scope.pre_third_list;
		$scope.fourth_list=$scope.pre_fourth_list;
		$scope.storage_list=$scope.pre_storage_list;
		$scope.servicer = $scope.pre_servicer;
		$scope.sender = $scope.pre_sender;
		$scope.receiver = $scope.pre_receiver;
		
		$scope.partmain_list = $scope.pre_partmain_list;
		$scope.repair_type = $scope.pre_repair_type;
		$scope.model_group = $scope.pre_model_group;
		$scope.charge_partition = $scope.pre_charge_partition;
		
		//收费区分
		$("#charge_partition_code").html("");
		$.each($scope.charge_partition,function(i,o){
			$("#charge_partition_code").append("<option value='"+o.charge_partition_code+"'>"+o.charge_partition_name+"</option>")
		})
		$("#charge_partition_code").multiselect({ 
			noneSelectedText: "==请选择==", 
			checkAllText: "全选", 
			uncheckAllText: '全不选', 
			selectedList:2,
			minWidth:"100%"
	    });
		$("#charge_partition_code").multiselect('refresh');
		
		//维修类型
		$("#repair_type_code").html("");
		$.each($scope.repair_type,function(i,o){
			$("#repair_type_code").append("<option value='"+o.repair_type_code+"'>"+o.repair_type_name+"</option>")
		})
		$("#repair_type_code").multiselect({ 
			noneSelectedText: "==请选择==", 
			checkAllText: "全选", 
			uncheckAllText: '全不选', 
			selectedList:2,
			minWidth:"100%"
	    });
		$("#repair_type_code").multiselect('refresh');
		
		//工单类别
		$("#ro_type_code").html("");
		$.each($scope.ro_type,function(i,o){
			$("#ro_type_code").append("<option value='"+o.status_code+"'>"+o.status_desc+"</option>")
		})
		$("#ro_type_code").multiselect({ 
			noneSelectedText: "==请选择==", 
			checkAllText: "全选", 
			uncheckAllText: '全不选', 
			selectedList:2,
			minWidth:"100%"
	    });
		$("#ro_type_code").multiselect('refresh');
		
		//其他
		$("#part_other").html("");
			$("#part_other").append("<option value='balanceNotClose'>已结算未关单</option>" +
					"<option value='outNotSquare'>已出库未结清</option>" +
					"<option value='outNotClose'>已出库未关单</option>" +
					"<option value='outNotBalance'>已出库未结算</option>")
		$("#part_other").multiselect({ 
			noneSelectedText: "==请选择==", 
			checkAllText: "全选", 
			uncheckAllText: '全不选', 
			selectedList:2,
			minWidth:"100%"
	    });
		$("#part_other").multiselect('refresh');
	}
	
	//门店监听
	$rootScope.$watch('search_entity_name', function(newValue,oldValue,scope){
		if(newValue==''){
			$scope.clearGroupBrandEntity();
		}
	});
	
	 $scope.loadSecond=function(name){
	    	$scope.sub_list_sel=null;
	    	$scope.third_list_sel=null;
			$scope.fourth_list_sel=null;
			$scope.subGroupName = '';
			var secondList=new Array();
			var main_code='';
			$.each($scope.main_list,function(i,o){
				if(o.name==name){main_code=o.code;}
			})
			if(main_code!=''){
				$.each($scope.sub_list,function(i,o){
					if(o.main_code==main_code){
						secondList.push(o);
					}
				})
				$scope.sub_list_sel=secondList;
			}
	    }
	    
	    $scope.loadThird=function(name){
			$scope.third_list_sel=null;
			$scope.fourth_list_sel=null;
			$scope.thirdGroupName = '';
			var thirdList=new Array();
			var sub_code='';
			$.each($scope.sub_list,function(i,o){
				if(o.name==name){sub_code=o.code;}
			})
			if(sub_code!=''){
				$.each($scope.third_list,function(i,o){
					if(o.sub_code==sub_code){
						thirdList.push(o);
					}
				})
				$scope.third_list_sel=thirdList;
			}
		}
		
		
		$scope.loadFourth=function(name){
			$scope.fourth_list_sel=null;
			var fourthList=new Array();
			var sub_code='';
			$.each($scope.third_list,function(i,o){
				if(o.name==name){sub_code=o.code;}
			})
			if(sub_code!=''){
				$.each($scope.fourth_list,function(i,o){
					if(o.sub_code==sub_code){
						fourthList.push(o);
					}
				})
				$scope.fourth_list_sel=fourthList;
			}
		}
		
	AppUtil.LoadGroups();
	// 导出
	$scope.ExportData = function() {
		AppUtil.ExportData($scope.postData);
	};
	
    $scope.formatStr = function(input){
		var returnStr = "";
		if(input == undefined){
			return;
		}
		var itemArr = input.split(";");
		for (var i = 0; i < itemArr.length; i++) {
			var temp = itemArr[i].split(":");
			if(temp.length>1){
				returnStr += temp[1];
			}else{
				returnStr += itemArr[i];
			}
		}
	    return returnStr;
	}
    
    $scope.loadThird=function(name){
		$scope.third_list_sel=null;
		$scope.fourth_list_sel=null;
		$scope.thirdGroupName = '';
		var thirdList=new Array();
		var sub_code='';
		$.each($scope.sub_list,function(i,o){
			if(o.name==name){sub_code=o.code;}
		})
		if(sub_code!=''){
			$.each($scope.third_list,function(i,o){
				if(o.sub_code==sub_code){
					thirdList.push(o);
				}
			})
			$scope.third_list_sel=thirdList;
		}
	}
	
	
	$scope.loadFourth=function(name){
		$scope.fourth_list_sel=null;
		var fourthList=new Array();
		var sub_code='';
		$.each($scope.third_list,function(i,o){
			if(o.name==name){sub_code=o.code;}
		})
		if(sub_code!=''){
			$.each($scope.fourth_list,function(i,o){
				if(o.sub_code==sub_code){
					fourthList.push(o);
				}
			})
			$scope.fourth_list_sel=fourthList;
		}
	}
	
	// 验证正整数包括0 
	function checkPositiveNumber(value){
	    var bool= /^(0|[1-9][0-9]*)$/.test(value);
	    return bool;
	} 
});
app.register.service('DeliverSer', function($http, AppUtil) {

	// 查询数据
	this.list = function(postData) {
		AppUtil.loading();
		postData.start = (postData.page - 1 < 0 ? 0 : postData.page - 1)
				* postData.size;
		return $http.post('deliver/list', postData).error(function() {
			alertMsg("提示", "系统出错,请稍后重试.");
			AppUtil.remove_loading();
		});
	};
	
	
});
